<?php
namespace App\V3\V3_2_7\Controllers;

use App\Services\CommonService;
use App\V3\V3_2_7\Models\CloudDeviceModel;
use App\V3\V3_2_7\Models\CloudDevicePrinterJobModel;
use App\Http\Controllers\Controller;
use App\V3\V3_2_7\Models\CloudDevicePrinterModel;
use Illuminate\Http\Request;

/**
 * 云盒服务器，回调接口
 */
class CloudCallbackController extends Controller
{

    /**
     * 提交打印任务后，回调任务接口
     */
    public function printerJob(Request $request)
    {
        $postData   = $request->post();
        $deviceId   = isset($postData['device_id']) ? $postData['device_id'] : '';
        $taskId     = isset($postData['task_id']) ? $postData['task_id'] : '';
        $taskState  = isset($postData['task_state']) ? $postData['task_state'] : '';
        //$code       = isset($postData['code']) ? $postData['code'] : '';
        //$msg        = isset($postData['msg']) ? $postData['msg'] : '';

        //查询任务
        $jobModel   = CloudDevicePrinterJobModel::query()->where('device_id',$deviceId)
            ->where('cdpj_task_id',$taskId)
            ->first();

        if(empty($jobModel)){
            return CommonService::fail('数据不存在！');
        }

        if(strtoupper($taskState) == 'SUCCESS'){
            $cdjp_callback_status   = 1;//打印成功
        }else{
            $cdjp_callback_status   = 2;//打印失败

            //记录失败次数
            CloudDevicePrinterModel::query()->where('device_id',$deviceId)
                                                    ->where('cdp_port',$jobModel->cdp_port)
                                                    ->increment('cdp_printer_fail_times');
        }

        $jobModel->cdjp_callback_status = $cdjp_callback_status;
        $jobModel->cdjp_callback_log    = json_encode($postData);
        $jobModel->save();

        return CommonService::success('回调成功！');
    }


    /**
     * 打印云盒信息回调
     */
    public function device(Request $request)
    {
        $postData       = $request->post();
        $sign           = isset($postData['sign']) ? $postData['sign'] : '';
        unset($postData['sign']);
        $signLocal      = self::generateSign($postData);
        if(strcmp($sign,$signLocal) != 0){
            //return response()->json(['code' => 201]);
        }

        //固定为 device
        $type           = isset($postData['type']) ? $postData['type'] : '';
        $deviceId       = isset($postData['deviceId']) ? $postData['deviceId'] : '';
        $action         = isset($postData['action']) ? $postData['action'] : ''; //up: 云盒上线, down: 云盒下线
        $ts             = isset($postData['ts']) ? $postData['ts'] : '';
        $isFirstTime    = isset($postData['isFirstTime']) ? $postData['isFirstTime'] : '';//是否首次上线

        //固定为 usb
        $usb            = isset($postData['usb']) ? $postData['usb'] : '';//USB端口，取值 1,2,3,4


        //更新云盒
        if($type == 'device'){
            $deviceMode = CloudDeviceModel::query()->where('device_id',$deviceId)->first();
            if(empty($deviceMode)){
                return response()->json(['code' => 404,'设备不存在']);
            }else{
                $deviceMode->device_action_status = $action;
                $deviceMode->save();
            }
        }

        //更新打印机
        if ($type == 'usb'){
            $printerModel   = CloudDevicePrinterModel::query()->where('device_id',$deviceId)
                                                              ->where('cdp_port',$usb)
                                                              ->first();
            if(empty($printerModel)){
                return response()->json(['code' => 404,'打印机不存在']);
            }else{
                $printerModel->cdp_action_status = $action;
                $printerModel->save();
            }
        }


        return response()->json(['code' => 200]);
    }

    /*
     * 根据数组数据 生成签名
     */
    protected function generateSign($dataArr)
    {
        if(empty($dataArr)){
            return '';
        }

        $appSecret = config('app.lianke_cloud_callback_app_secret');

        ksort($dataArr);
        $signStr    = '';

        foreach ($dataArr as $k => $v){
            $signStr    .= "{$k}:{$v},";
        }

        $signStr    .= $appSecret;

        return hash('sha256',$signStr);
    }


}
